/**
 * @file memory.c
 * @brief Memory functions.
 * @version 1.0
 * @date Nov-2011
 * @author Leonardo Lino 2101101
 * @author Joaquim Ferreira 2101104
 */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

#include "debug.h"
#include "memory.h"

/** @brief This function alocate memory and check for errors.*/
void *my_malloc (size_t size, const int line, const char *file)
{

  void *ptr = malloc (size);

  if (ptr == NULL) {
    fprintf (stderr, "[%d@%s][ERROR] can't malloc %u bytes\n", line, file,
	     (int) size);
  }
  //printf ("ALOC: %p [%s@%d]\n", &ptr, file, line);
  return ptr;
}

/** @brief This function free the memory and check for errors.*/
void my_free (void **ptr, const int line, const char *file)
{

  if (*ptr == NULL) {
    fprintf (stderr, "[%d@%s][ERROR] Pointer is NULL\n", line, file);
  } else {
    //printf ("FREE: %p [%s@%d]\n", &(*ptr), file, line);
    free (*ptr);
    *ptr = NULL;
  }
}
